package com.fzy.core.entity;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fzy.core.utils.NumberUtils;
import com.fzy.core.utils.U;

public class Page {
	private static final Logger log = LoggerFactory.getLogger(Page.class);
	public static final int DEFAULT_PAGE_SIZE = 10;
	public static final int DEFAULT_PAGE_NO = 1;
	
	public static final String ASC = "asc";
	public static final String DESC = "desc";

	protected String pageNo;
	protected String pageSize;
	protected String orderBy;
	protected String order;

	protected List result;
	protected long totalCount;

	public long getPageNoLong() {
		return NumberUtils.toLong(pageNo, DEFAULT_PAGE_NO);
	}

	public long getPageSizeLong() {
		return NumberUtils.toLong(pageSize, DEFAULT_PAGE_SIZE);
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		correctPageNo();
		this.pageNo = pageNo;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		correctPageSize();
		this.pageSize = pageSize;
	}

	public long getFirst() {
		long s = getPageSizeLong();
		long n = getPageNoLong();
		return ((n - 1) * s) + 1;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public Page orderBy(final String theOrderBy) {
		setOrderBy(theOrderBy);
		return this;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		if(order == null) return;
		String lowcaseOrder = StringUtils.lowerCase(order);

		String[] orders = StringUtils.split(lowcaseOrder, ',');
		for (int i = 0; i < orders.length; i++) {
			String orderStr = orders[i];
			if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
				throw new IllegalArgumentException("order " + orderStr + " is illegal");
			}
		}
		this.order = lowcaseOrder;
	}

	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
	}
	public List getResult() {
		return result;
	}

	public void setResult(final List result) {
		this.result = result;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(final long totalCount) {
		this.totalCount = totalCount;
		correctPageNoByTotalCount();
	}

	public long getTotalPages() {
		long s = getPageSizeLong();
		long n = getPageNoLong();
		if (totalCount < 0) {
			return 0;
		}

		long count = totalCount / s;
		if (totalCount % s > 0) {
			count++;
		}
		return count;
	}

	public boolean isHasNext() {
		long n = getPageNoLong();

		return (n + 1 <= getTotalPages());
	}

	public long getNextPage() {
		long n = getPageNoLong();

		if (isHasNext()) {
			return n + 1;
		} else {
			return n;
		}
	}

	public boolean isHasPre() {
		long n = getPageNoLong();
		return (n - 1 >= 1);
	}

	public long getPrePage() {
		long n = getPageNoLong();
		if (isHasPre()) {
			return n - 1;
		} else {
			return n;
		}
	}
	
	public void initFormList(List list){
		long s = getPageSizeLong();
		long first = getFirst();
		int fromIndex = (int)(first - 1);
		int toIndex = (int)(first + s - 1);
		toIndex = toIndex > list.size() ? list.size():toIndex;
		if(list != null && list.size() >= fromIndex){
			setTotalCount(list.size());
			setResult(list.subList(fromIndex,toIndex));
		}
	}
	
	public void correctPageNoByTotalCount(){
		correctPageSize();
		long s = getPageSizeLong();
		long n = getPageNoLong();
		if(n < 1) n = 1;
		if(totalCount > -1){
			if(((n - 1) * s) + 1 > totalCount){
				n = (long)(totalCount / s);
				if(((n - 1) * s) + 1 + s <= totalCount) n += 1;
				if(n < 1) n = 1;
			}
		}
		pageNo  = n + "";
	}
	
	private void correctPageNo(){
		long n = getPageNoLong();
		if(n < 1) n = 1;
		pageNo  = n + "";
	}
	
	private void correctPageSize(){
		long s = getPageSizeLong();
		if(s < 1) s = 1;
		pageSize = s + "";
	}
}
